﻿using JmShared;
using SuperWebSocket.SubProtocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebSocketServer.Filters;

namespace WebSocketServer.Commands
{
    [LoggedInValidationFilter(Order = 0)]
    [LogTimeCommandFilter(Order = 1)]
   public class RequestEmoticon:JsonSubCommand<JiveClientSession,EmoticonRequest>
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        protected override void ExecuteJsonCommand(JiveClientSession session, EmoticonRequest request)
        {
            ContactEmoticonTransfer emoticon = DB.DbManager.ReadCustomEmoticon(request.DeviceID,request.Username,request.KeyWord);
            if (emoticon != null)
            {
                if (session.SendEmoticon(emoticon))
                    log.Debug(string.Format("Sent emoticon {0} from {1} to {2}", emoticon.KeyWord, emoticon.SenderID, session.ChatClient.Username));
                else
                    log.Warn(string.Format("Failed to send emoticon to user {0}", session.ChatClient.Username));
            }
            else
                log.Warn(string.Format("Could not find emoticon with keyword {0} for user {1} in database. Send aborted",request.KeyWord, request.Username));
        }
    }
}
